package com.example.testapp.activity.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.testapp.R;
import com.example.testapp.activity.activity.study.PhotoTechDetailActivity;
import com.example.testapp.activity.adapter.IndexIntoAdapter;
import com.example.testapp.activity.http.AsyncHttpUtil;
import com.example.testapp.activity.http.Contacts;
import com.example.testapp.activity.model.ArticleItem;
import com.example.testapp.activity.model.FocusPicture;
import com.example.testapp.activity.model.PhonePaging;
import com.example.testapp.activity.ui.DataTask;
import com.example.testapp.activity.util.JsonUtil;
import com.example.testapp.activity.util.JsonUtils;
import com.example.testapp.activity.util.MD5Utils;
import com.example.testapp.activity.view.ErrorHintView;
import com.example.testapp.activity.view.listview.PullToRefreshAdapterViewBase;
import com.example.testapp.activity.view.listview.PullToRefreshBase;
import com.example.testapp.activity.view.listview.PullToRefreshListView;
import com.example.testapp.activity.view.pager.TestViewPager;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
 * Created by l on 2016/8/25.
 */
public class PhoneTechFragment extends BaseFragment {

    private View view;
    private TestViewPager  mViewPager;
    private LinearLayout   mIndicator;
    private ErrorHintView  mErrorHintView;
    private PullToRefreshListView mListView;
    private RelativeLayout  mIndexrl;
    private ImageView   mIndexImage;

    private MyPagerAdapter pagerAdapter;
    private List<FocusPicture> pictureList = new ArrayList<FocusPicture>();  //广告界面的数据源
    private ImageView[]  mIndicators; /** 装指引的ImageView数组 **/
    /** 装ViewPager中ImageView的数组 **/
    private ImageView[] mImageViews;
    /** 图片自动切换时间 */
    private static final int PHOTO_CHANGE_TIME = 2000;
    private static final int MSG_CHANGE_PHOTO = 1;

    private List<ArticleItem> ArticleData = new ArrayList<ArticleItem>();
    private IndexIntoAdapter indexAdapter;

    private boolean isViewPager = true;
    private int offset = 0;				//第N条数据
    private boolean isPage = true;		//是否还有下一页
    private LinearLayout footerView;    //最后一条listview

    public static int VIEW_LIST = 1;
    /**显示断网**/
    public static int VIEW_WIFIFAILUER = 2;
    /** 显示加载数据失败 **/
    public static int VIEW_LOADFAILURE = 3;
    public static int VIEW_LOADING = 4;
    public static int VIEW_INDEXIMAGE = 5;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case MSG_CHANGE_PHOTO:
                    int index = mViewPager.getCurrentItem();
                    if (index == pictureList.size() - 1){
                        index = -1;
                    }
                    mViewPager.setCurrentItem(index + 1);
                    mHandler.sendEmptyMessageDelayed(MSG_CHANGE_PHOTO, PHOTO_CHANGE_TIME);
                    break;
            }
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_phone_tech, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        mViewPager = (TestViewPager)view.findViewById(R.id.phone_viewpager);
        mIndicator = (LinearLayout)view.findViewById(R.id.indicator);
        mListView = (PullToRefreshListView)view.findViewById(R.id.list);
        mErrorHintView = (ErrorHintView)view.findViewById(R.id.hintView);
        mIndexrl = (RelativeLayout)view.findViewById(R.id.index_bg);
        mIndexImage = (ImageView)view.findViewById(R.id.index_image);
        mIndexrl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                  showLoading(VIEW_LIST);
            }
        });
        setupIdleListener();

        showLoading(VIEW_LOADING);
        refreshData(true);
    }

    /**
     * 初始化listview
     */
    private void setupIdleListener() {
        indexAdapter = new IndexIntoAdapter(ArticleData, mActivity);
        mListView.setAdapter(indexAdapter);

        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                if(refreshView.isHeaderShown()){
                    defParams();
                    showLoading(VIEW_LOADING);
                    refreshData(true);
                }
            }
        });

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ArticleItem item = ArticleData.get(position - 1);
                if(item != null && item.getType().equals("wz")){
                    Intent intent = new Intent(mContext, PhotoTechDetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("ArticleItem", item);
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                }
            }
        });

        mListView.setOnPreRefreshingAnimListener(new PullToRefreshAdapterViewBase.OnPreRefreshingAnimListener() {
            @Override
            public void onPreRefreshingAnim() {
                 mListView.setFooterLoadingViewHeaderText("加载更多资讯");
            }
        });

        mListView.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                if(isPage){
                  //滑动到底部自动刷新
                  refreshData(false);
                }else{
                    //没有下一页了
                    mListView.onRefreshComplete();
                    if(footerView == null){
                        footerView = (LinearLayout)View.inflate(mContext, R.layout.item_pull_to_refresh_footer, null);
                        TextView footer = (TextView)footerView.findViewById(R.id.footer);
                        footer.setText("到底了呢-共" + ArticleData.size() + "条资讯");
                        mListView.getRefreshableView().addFooterView(footerView);
                    }
                }
            }
        });
    }

    private void showLoading(int i) {
        mErrorHintView.setVisibility(View.GONE);
        mListView.setVisibility(View.GONE);
        mViewPager.setVisibility(View.GONE);
        mIndicator.setVisibility(View.GONE);
        mIndexrl.setVisibility(View.GONE);

        switch (i){
            case 1:
                mErrorHintView.hideLoading();
                mViewPager.setVisibility(View.VISIBLE);
                mIndicator.setVisibility(View.VISIBLE);
                mListView.setVisibility(View.VISIBLE);
                break;
            case 2:
                mErrorHintView.hideLoading();
                mErrorHintView.netError(new ErrorHintView.OperateListener() {
                    @Override
                    public void operate() {
                        defParams();
                        showLoading(VIEW_LOADING);
                        refreshData(true);
                    }
                });
                break;
            case 3:
                mErrorHintView.hideLoading();
                mErrorHintView.loadFailure(new ErrorHintView.OperateListener() {
                    @Override
                    public void operate() {
                        defParams();
                        showLoading(VIEW_LOADING);
                        refreshData(true);
                    }
                });
                break;
            case 4:
                mErrorHintView.loadingdata();
                break;
            case 5:
                mErrorHintView.hideLoading();
                mIndexrl.setVisibility(View.VISIBLE);
                break;
        }
    }

    /**
     * 恢复默认参数
     */
    private void defParams() {
        offset = 0;
        isPage = true;
    }

    private void refreshData(boolean clean) {
        dataTask = new RefreshDataTask(dataTask.getID() + 1, clean);
        dataTask.run();
    }

    public class RefreshDataTask extends DataTask{

        public boolean clean = false;		// 是否清空原数据
        public RefreshDataTask(int id, boolean flag) {
            super(id);
            this.clean = flag;
        }

        @Override
        public void run() {
            // 判断是不是用户的最后操作,最后任务的ID如果比此任务的ID大则丢弃请求结果
            if(getID() < dataTask.getID()){
                 return;
            }else {
                loadIndexInfo(clean);
            }
        }
    }

    /**
     * 加载首页内容
     * @param flag
     */
    private void loadIndexInfo(final boolean flag) {
        RequestParams params = new RequestParams();
        String t = String.valueOf(System.currentTimeMillis() / 1000); //获取当前时间
        params.put("timestamp", t);
        String token1 = MD5Utils.md5("d19cf361181f5a169c107872e1f5b722" + t);
        params.put("token1", token1);

        params.put("apiid", 3);
        params.put("module", "api_libraries_sjdbg_indexrecommend");
        params.put("returnformat", "json");
        params.put("encoding", "utf8");
//		params.put("onetime", timeStamp);			//分页用，传第一条新闻的时间戳，第一页下不用传值。
        params.put("offset", offset );				//分页用，数据记录的起始行数。0为第一条记录。
        params.put("rows", 10);						//分页用，数据记录每次取得的行数。不传此参数则默认获取10条记录。

        AsyncHttpUtil.get(Contacts.PHONE_URL, params, new AsyncHttpResponseHandler(){

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if(footerView != null){
                    mListView.getRefreshableView().removeFooterView(footerView);
                    footerView = null;
                }

                mListView.onRefreshComplete();

                if(responseBody != null && responseBody.length > 0){
                    try {
                        JSONObject obj = new JSONObject(new String(responseBody));
                        String  status = obj.getString("status");
                        if(TextUtils.equals("1", status)){
                            offset += 10;

                            //广告图片
                            JSONObject data = obj.getJSONObject("data");
                            JSONArray focusPicture = data.getJSONArray("focusPicture");
                            if(isViewPager){
                                if(focusPicture != null){
                                    List<FocusPicture> instance = JsonUtils.getInstance(FocusPicture.class, focusPicture);
                                    if(instance != null && instance.size() > 0){
                                        pictureList.addAll(instance);
                                        initViewPager();
                                        isViewPager = false;
                                    }
                                }
                            }

                            //文章列表
                            JSONObject articleList = data.getJSONObject("articleList");
                            JSONArray  list = articleList.getJSONArray("list");
                            if(list != null){
                                List<ArticleItem> instance = JsonUtils.getInstance(ArticleItem.class, list);
                                if(instance != null && instance.size() > 0){
                                    if(flag){
                                        ArticleData.clear();
                                    }
                                    ArticleData.addAll(instance);
                                    showLoading(VIEW_LIST);
                                    indexAdapter.notifyDataSetChanged();
                                }else {
                                    showLoading(VIEW_LOADFAILURE);
                                }
                            }

                            JSONObject paging = articleList.getJSONObject("paging");
                            if(paging != null){
                                PhonePaging instance = JsonUtils.getInstance(PhonePaging.class, paging);
                                if(instance != null){
                                    try {
                                        int total = Integer.parseInt(instance.getTotal());
                                        if(offset >= total){
                                            isPage = false;
                                        }else {
                                            isPage = true;
                                        }
                                    }catch (Exception e){
                                        isPage = true;
                                    }
                                }
                            }
                        }else{
                            showLoading(VIEW_LOADFAILURE);
                        }
                    }catch (Exception e) {
                         showLoading(VIEW_LOADFAILURE);
                    }
                }else {
                    showLoading(VIEW_LOADFAILURE);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                  showLoading(VIEW_WIFIFAILUER);
            }
        });
    }

    /**
     *  初始化ViewPager
     */
    private void initViewPager() {
        mIndicators = new ImageView[pictureList.size()];
        if(pictureList.size() <= 1){
            mIndicator.setVisibility(View.GONE);
        }

        for(int i = 0; i < mIndicators.length; i++){
            ImageView imageView = new ImageView(mContext);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
            if(i != 0){
                params.leftMargin = 5;
            }
            imageView.setLayoutParams(params);
            mIndicators[i] = imageView;
            if(i == 0){
                mIndicators[i].setBackgroundResource(R.mipmap.android_activities_cur);
            }else {
                mIndicators[i].setBackgroundResource(R.mipmap.android_activities_gap);
            }
            mIndicator.addView(imageView);
        }

        mImageViews = new ImageView[pictureList.size()];
        for(int i = 0; i < mImageViews.length; i++){
            ImageView imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            mImageViews[i] = imageView;
        }

        mViewPager.setTransitionEffect(TestViewPager.TransitionEffect.CubeOut);
        mViewPager.setCurrentItem(0);
        mHandler.sendEmptyMessageDelayed(MSG_CHANGE_PHOTO, PHOTO_CHANGE_TIME);

        pagerAdapter = new MyPagerAdapter();
        mViewPager.setAdapter(pagerAdapter);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {  }

            @Override
            public void onPageSelected(int position) {
                setImageBackground(position);
                final FocusPicture item = pictureList.get(position);
                mImageViews[position].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(!TextUtils.isEmpty(item.getPicUrl())){
                            showProgressDialog();
                            showLoading(VIEW_INDEXIMAGE);
                            Picasso.with(mContext).load(item.getPicUrl()).placeholder(R.color.ECECEC)
                                    .error(R.color.ECECEC).into(mIndexImage);
                            dismissProgressDialog();
                        }
                    }
                });
            }

            @Override
            public void onPageScrollStateChanged(int state) {   }
        });

        mViewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(pictureList.size() == 0 || pictureList.size() == 1)
                   return true;
                else
                  return false;
            }
        });
    }

    /**
     * 设置选中的tip的背景
     *
     * @param selectItemsIndex
     */
    private void setImageBackground(int selectItemsIndex) {
        for(int i = 0; i < mIndicators.length; i++){
            if(i == selectItemsIndex){
                mIndicators[i].setBackgroundResource(R.mipmap.android_activities_cur);
            }else {
                mIndicators[i].setBackgroundResource(R.mipmap.android_activities_gap);
            }
        }
    }

    public class MyPagerAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return mImageViews.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager)container).removeView(mViewPager.findViewFromObject(position));
        }

        @Override
        public Object instantiateItem(View container, int position) {
            FocusPicture picture = pictureList.get(position);
            if(!TextUtils.isEmpty(picture.getPicUrl())){
                Picasso.with(mContext).load(picture.getPicUrl()).placeholder(R.color.ECECEC).error(R.color.ECECEC).into(mImageViews[position]);
            }
            ((ViewPager)container).addView(mImageViews[position], 0);
            mViewPager.setObjectForPosition(mImageViews[position], position);
            return mImageViews[position];
        }
    }
}
